<template>
  <zero-dialog :showDialog.sync="addUpdateFormVisible"
               :fullscreen.sync="fullscreen"
               :btnExist="btnExist"
               :loading="loading"
               :title="title"
               :submit="addOrupdate">
    <div slot="body">
      <el-form :model="addUpdateForm"
               :rules="rules"
               label-width="80px"
               ref="addUpdateFormRef">

        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item prop="name"
                          label="名称">
              <el-input v-model.trim="addUpdateForm.name"
                        placeholder="请输入名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item prop="databaseType"
                          label="类型">
              <el-select v-model="addUpdateForm.databaseType"
                         placeholder="请选择类型"
                         style="width: 100%">
                <el-option key="MYSQL"
                           value="MYSQL"
                           label="MYSQL"></el-option>
                <el-option key="SQL_SERVER"
                           value="SQL_SERVER"
                           label="SQL_SERVER"></el-option>
                <el-option key="ORACLE"
                           value="ORACLE"
                           label="ORACLE"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item prop="username"
                          label="用户名">
              <el-input v-model.trim="addUpdateForm.username"
                        placeholder="请输入用户名" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item prop="password"
                          label="密码">
              <el-input v-model.trim="addUpdateForm.password"
                        placeholder="请输入密码"
                        show-password />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item prop="ipPort"
                          label="IP和端口">
              <el-input v-model.trim="addUpdateForm.ipPort"
                        placeholder="请输入IP和端口" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="其他参数">
              <el-input type="textarea"
                        :rows="2"
                        v-model.trim="addUpdateForm.otherParams"
                        placeholder="请输入其他参数" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="备注">
          <el-input type="textarea"
                    v-model="addUpdateForm.remarks"
                    placeholder="请输入备注" />
        </el-form-item>
      </el-form>
    </div>
  </zero-dialog>
</template>

<script>
export default {
  name: 'AddUpdateForm',
  props: {
    addUrl: String,
    updateUrl: String,
    getDataList: {
      type: Function,
      default: null
    }
  },
  data () {
    return {
      loading: false,
      fullscreen: false, // 弹框全屏
      btnExist: true, // 按钮是否存在
      updateExist: true, // 修改时，是否存在的输入框
      addUpdateFormVisible: false, // 是否显示弹框
      title: '', // 弹框标题
      addUpdateForm: {},
      initFrom: {
        state: 1,
        type: '0'
      },
      rules: {
        name: [{ required: true, message: '请输入名称', trigger: 'blur' }],
        databaseType: [{ required: true, message: '请选类型', trigger: 'blur' }],
        username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
        password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
        ipPort: [{ required: true, message: '请输入IP和端口', trigger: 'blur' }]
      }
    }
  },
  methods: {
    initDialog (row) {
      // 新增或修改
      this.addUpdateFormVisible = true
      this.addUpdateFormReset()
      if (row) {
        this.updateExist = false
        row.databaseType += '' // 转成字符串,不然select控件不支持回显
        this.title = '修改数据库配置'
        this.addUpdateForm = JSON.parse(JSON.stringify(row))
      } else {
        this.updateExist = true
        this.title = '新增数据库配置'
      }
    },
    addOrupdate () {
      // 添加或修改
      this.$refs.addUpdateFormRef.validate(valid => {
        if (valid) {
          var oldAddUpdateForm = JSON.parse(JSON.stringify(this.addUpdateForm))
          oldAddUpdateForm.password = this.$getRsaCode(oldAddUpdateForm.password)
          this.loading = true
          this.btnExist = false
          const config = {
            url: oldAddUpdateForm.id ? this.updateUrl : this.addUrl,
            method: 'post',
            data: oldAddUpdateForm
          }
          this.$axios(config)
            .then(res => {
              this.loading = false
              this.addUpdateFormVisible = false
              this.btnExist = true
              this.getDataList()
              this.$message.success(res.msg)
            })
            .catch(() => {
              this.loading = false
              this.btnExist = true
            })
        }
      })
    },
    addUpdateFormReset () {
      // 重置表单 需要转成字符串赋值，不然这两个对象的值会一直相等
      this.addUpdateForm = JSON.parse(JSON.stringify(this.initFrom))
      // 重置表单
      this.$nextTick(() => {
        this.fullscreen = false
        this.$refs.addUpdateFormRef.clearValidate()
      })
    }
  }
}
</script>
